Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ClubStarterKit.Data
Imports ClubStarterKit.Web

Partial Class Poll_View
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            PollControl1.SelectedPollId = New Guid(Request("PollId"))
        Catch
            Response.Redirect("~/Poll/List.aspx", True)
        End Try
        If PollControl1.SelectedPollId = Guid.Empty Then
            Response.Redirect("~/Poll/List.aspx", True)
        End If

        'Only show AddReaction Linkbuttons if the user is authenticated
        AddReactionLink1.Visible = Page.User.Identity.IsAuthenticated
        AddReactionLink2.Visible = Page.User.Identity.IsAuthenticated

        Repeater1.DataSource = Poll.GetReactionsForPollQuestion(PollControl1.SelectedPollId)
        Repeater1.DataBind()
    End Sub

    Protected Sub PollLink_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect(String.Format("~/Poll/AddReaction.aspx?pollId={0}", PollControl1.SelectedPollId.ToString()), True)
    End Sub

    Protected Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If User.IsInRole("Administrators") Then
            Dim reactionId As New Guid((DirectCast(sender, LinkButton)).CommandArgument.ToString())
            Poll.DeleteReaction(reactionId)
            Response.Redirect("~/Poll/View.aspx?PollId=" & PollControl1.SelectedPollId.ToString())
        End If
    End Sub

    Public Function IsAdmin() As Boolean
        Return User.IsInRole("Administrators")
    End Function
End Class
